<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}系统权限`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.rowData"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="上级菜单" prop="pid">
        <el-tree-select
          v-model="drawerProps.rowData.pid"
          :data="drawerProps.authTree"
          :props="{ label: 'title', value: 'id' }"
          :default-expanded-keys="[drawerProps.rowData.pid]"
          :check-strictly="true"
          value-key="id"
          node-key="id"
          filterable
          clearable
        />
      </el-form-item>
      <el-form-item label="图标类型" prop="icon_type">
        <el-select v-model="drawerProps.rowData.icon_type" placeholder="请选择图标类型">
          <el-option label="iconify图标" :value="1" />
          <el-option label="本地图标" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="图标" prop="icon_name">
        <SelectIcon v-if="drawerProps.rowData.icon_type == 1" v-model:iconValue="drawerProps.rowData.icon_name" />
        <el-input v-else v-model="drawerProps.rowData.icon_name" placeholder="分类图标" clearable />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="drawerProps.rowData.title" placeholder="菜单标题" clearable />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="drawerProps.rowData.name" placeholder="菜单名称(英文)" clearable />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="drawerProps.rowData.type" @change="handleTypeChange" placeholder="菜单类型" clearable>
          <el-option v-for="item in authTypeEnum" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="drawerProps.rowData.type == 3" label="是否外链" prop="iframe">
        <el-radio-group v-model="drawerProps.rowData.iframe" @change="handleIframeChange">
          <el-radio :value="1">是</el-radio>
          <el-radio :value="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="drawerProps.rowData.type == 3 && drawerProps.rowData.iframe == 1" label="链接地址" prop="link">
        <el-input v-model="drawerProps.rowData.link" placeholder="链接地址" clearable />
      </el-form-item>
      <el-form-item v-if="drawerProps.rowData.type == 3 && drawerProps.rowData.iframe == 0" label="路由地址" prop="router">
        <el-input v-model="drawerProps.rowData.router" placeholder="路由地址" clearable />
      </el-form-item>
      <el-form-item v-if="drawerProps.rowData.type == 3 && drawerProps.rowData.iframe == 0" label="组件地址" prop="component">
        <el-input v-model="drawerProps.rowData.component" placeholder="组件地址" clearable />
      </el-form-item>
      <el-form-item v-if="drawerProps.rowData.type == 3 && drawerProps.rowData.iframe == 0" label="是否缓存" prop="keep_alive">
        <el-radio-group v-model="drawerProps.rowData.keep_alive">
          <el-radio :value="1">是</el-radio>
          <el-radio :value="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="drawerProps.rowData.type == 3 && drawerProps.rowData.iframe == 0" label="是否隐藏" prop="hidden">
        <el-radio-group v-model="drawerProps.rowData.hidden">
          <el-radio :value="1">是</el-radio>
          <el-radio :value="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="drawerProps.rowData.type == 4" label="权限标识" prop="auth_key">
        <el-input v-model="drawerProps.rowData.auth_key" placeholder="权限标识" clearable />
      </el-form-item>
      <el-form-item v-if="drawerProps.rowData.id > 0" label="排序" prop="router">
        <el-input-number :min="0" v-model="drawerProps.rowData.sort" placeholder="菜单排序" clearable />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="drawerProps.rowData.status">
          <el-radio :value="1">启用</el-radio>
          <el-radio :value="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { authTypeEnum } from "@/config/enums/serviceDict.ts";
import { ElMessage, FormInstance, ElTreeSelect } from "element-plus";
import SelectIcon from "@/components/SelectIcon/index.vue";

const rules = reactive<Record<string, any[]>>({
  title: [{ required: true, message: "请填写菜单标题" }],
  name: [{ required: true, message: "请填写菜单名称" }],
  type: [{ required: true, message: "请选择菜单类型" }],
  status: [{ required: true, message: "请选择菜单状态" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  authTree: Array<any>;
  rowData?: any;
  api?: (params: any) => Promise<any>;
  getTableList?: () => Promise<any>;
}

// drawer框状态
const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  authTree: []
});

const handleTypeChange = (type: number) => {
  if (type == 3) {
    rules["iframe"] = [{ required: true, message: "请选择是否外链" }];
  } else if (type == 4) {
    rules["auth_key"] = [{ required: true, message: "请填写权限标识" }];
  } else {
    delete rules["iframe"];
    delete rules["auth_key"];
  }
}

const handleIframeChange = (iframe: number) => {
  if (iframe == 0) {
    delete rules["link"];
    rules["router"] = [{ required: true, message: "请填写路由地址" }];
    rules["component"] = [{ required: true, message: "请填写组件地址" }];
  } else {
    delete rules["router"];
    delete rules["component"];
    rules["link"] = [{ required: true, message: "请填写链接地址" }];
  }
}

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps): void => {
  drawerProps.value = params;
  drawerVisible.value = true;
  if (drawerProps.value.rowData.id) {
    handleTypeChange(drawerProps.value.rowData.type);
    if (drawerProps.value.rowData.type == 3) {
      handleIframeChange(drawerProps.value.rowData.iframe);
    }
  }
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      delete drawerProps.value.rowData.children;
      if (!drawerProps.value.rowData.pid) {
        drawerProps.value.rowData.pid = 0;
      }
      await drawerProps.value.api!(drawerProps.value.rowData);
      ElMessage.success({ message: `${drawerProps.value.title}系统权限成功！` });
      if (drawerProps.value.getTableList) {
        await drawerProps.value.getTableList();
      }
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
